package com.common.project.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;


/**
 * 填充器
 * @author fulin
 * @since 2018-09-12
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Class<?> aClass = metaObject.getOriginalObject().getClass();
        Field[] fields = aClass.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            if(fields[i].getName().equals("createTime")){
//                metaObject.setValue("createTime",LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()));
                metaObject.setValue("createTime",new Date());
                break;
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Class<?> aClass = metaObject.getOriginalObject().getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            if(fields[i].getName().equals("updateTime")){
//                metaObject.setValue("updateTime", LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()));
                metaObject.setValue("updateTime", new Date());
                break;
            }
        }

    }

}


